package com.dayi.utils.pop;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.dayi.R;
import com.lib.fastkit.utils.log.LogUtil;
import com.lib.fastkit.views.dialog.zenhui.AlertDialog;
import com.lib.fastkit.views.recyclerview.tool.MyLinearLayoutManager;
import com.lib.utls.glide.GlideConfig;
import com.zyyoona7.popup.EasyPopup;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class AskChoosePopupUtils implements View.OnClickListener {

    private Activity activity;

    private EasyPopup choosePopup;

//    private LinearLayout linGrade, linSubj;

//    private TextView tvGrade, tvSubj;

    private ImageView iv_hide;

    private Spinner spinGrade, spinSubj;


    private TextView tvMoney;
    private LinearLayout linPay;

    private String[] gradeArr, subjArr;

    public AskChoosePopupUtils(Activity activity) {
        this.activity = activity;

        initAskChoose();

    }


    private void initAskChoose() {

        choosePopup = EasyPopup.create()
                .setContext(activity)
                .setContentView(R.layout.dialog_askchoose_1)
                .setWidth(WindowManager.LayoutParams.MATCH_PARENT)
                .setHeight(WindowManager.LayoutParams.MATCH_PARENT)
                .setOnViewListener(new EasyPopup.OnViewListener() {
                    @Override
                    public void initViews(View view, EasyPopup basePopup) {

                        initWidget(view);

                    }

                })
                .setFocusAndOutsideEnable(true)
                .apply();

        iv_hide.setOnClickListener(this);
//        linGrade.setOnClickListener(this);
//        linSubj.setOnClickListener(this);
        linPay.setOnClickListener(this);

    }

    private void initWidget(View view) {

//        linGrade = view.findViewById(R.id.lin_grade);
//        tvGrade = view.findViewById(R.id.tv_grade);
//        ivGrade = view.findViewById(R.id.iv_grade);
//
//        linSubj = view.findViewById(R.id.lin_subj);
//        tvSubj = view.findViewById(R.id.tv_subj);
//        ivSubj = view.findViewById(R.id.iv_subj);


        spinGrade = view.findViewById(R.id.spin_grade);
        spinSubj = view.findViewById(R.id.spin_subj);


        iv_hide = view.findViewById(R.id.iv_hide);

        tvMoney = view.findViewById(R.id.tv_money);
        linPay = view.findViewById(R.id.lin_pay);

        gradeArr = activity.getResources().getStringArray(R.array.array_choose_grade);
        subjArr = activity.getResources().getStringArray(R.array.array_choose_subject);


        spinGrade.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                LogUtil.e("点击开始录制");
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

//        spinGrade.setData(putArrToList(gradeArr));
//        spinSubj.setData(putArrToList(subjArr));

    }

    public List<String> putArrToList(String[] arr) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            list.add(arr[i]);
        }
        return list;

    }


    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.iv_hide) {

            dismiss();

        } else if (i == R.id.lin_grade) {

//            if (gradeListener != null) {
//                gradeListener.showGradePop(view);
//            }
//            dismiss();

        } else if (i == R.id.lin_subj) {

//            GradeSubjSelectPopupUtils selectPopupUtils = new GradeSubjSelectPopupUtils(activity);
//            selectPopupUtils.updateSelect(subjArr);
//
//            selectPopupUtils.setSelectItemPopupUtilsListener(new GradeSubjSelectPopupUtils.SelectItemPopupUtilsListener() {
//                @Override
//                public void selectItem(String itemText) {
//
//                    tvSubj.setText(itemText);
//                }
//            });
//            selectPopupUtils.showSelectPopuPopu(view);

        } else if (i == R.id.lin_pay) {

            if (listener != null) {
                listener.payMoney();
            }

            dismiss();
        }

    }


    public void showAnswerPopuPopu(View view) {

        choosePopup.showAtLocation(view, Gravity.CENTER, 0, 0);
    }

    public void dismiss() {

        choosePopup.dismiss();
    }


    /**
     * interface
     */
    private ChooseGradePopupUtilsListener gradeListener;

    public void setChooseGradePopupUtilsListener(ChooseGradePopupUtilsListener gradeListener) {

        this.gradeListener = gradeListener;

    }


    public interface ChooseGradePopupUtilsListener {

        void showGradePop(View view);
    }


    /**
     * interface
     */
    private ChoosePayPopupUtilsListener listener;

    public void setChoosePayPopupUtilsListener(ChoosePayPopupUtilsListener askChoosePopupUtilsListener) {

        this.listener = askChoosePopupUtilsListener;

    }


    public interface ChoosePayPopupUtilsListener {

        void payMoney();
    }





    private void showGradeDialog() {

        View view = LayoutInflater.from(activity).inflate(R.layout.popup_gradesubjselect, null);

        final AlertDialog dialog = new AlertDialog.Builder(activity)
                .setContentView(view)
                .setCancelable(true)
                .show();

        RecyclerView rvSelect = view.findViewById(R.id.rv_select);

        SelectAdapter selectAdapter = new SelectAdapter(gradeArr);
        MyLinearLayoutManager manager = new MyLinearLayoutManager(activity);
        rvSelect.setLayoutManager(manager);
        rvSelect.setAdapter(selectAdapter);


    }

    class SelectAdapter extends RecyclerView.Adapter<SelectViewHolder> {

        private String[] selectData;

        public SelectAdapter(String[] data) {
            this.selectData = data;
        }

        @Override
        public SelectViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {

            View view = LayoutInflater.from(activity).inflate(R.layout.item_select, null);

            return new SelectViewHolder(view);
        }

        @Override
        public void onBindViewHolder(SelectViewHolder selectViewHolder, final int position) {

            selectViewHolder.tvText.setText(selectData[position]);
            selectViewHolder.linItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

        }

        @Override
        public int getItemCount() {
            return selectData.length;
        }
    }


    class SelectViewHolder extends RecyclerView.ViewHolder {

        private TextView tvText;
        private LinearLayout linItem;

        public SelectViewHolder(View itemView) {
            super(itemView);

            tvText = itemView.findViewById(R.id.tv_text);
            linItem = itemView.findViewById(R.id.lin_item);

        }
    }

}




